package problem530;

//530. 二叉搜索树的最小绝对差
//https://leetcode.cn/problems/minimum-absolute-difference-in-bst/?envType=study-plan-v2&envId=top-interview-150
class Solution {
    int ret = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if(root == null || (root.left == null && root.right == null)) return 0;

        TreeNode cur = root.left;
        while(cur != null) {
            ret = Math.min(ret, Math.abs(root.val-cur.val));
            cur = cur.right;
        }
        cur = root.right;
        while(cur != null) {
            ret = Math.min(ret, Math.abs(root.val-cur.val));
            cur = cur.left;
        }
        getMinimumDifference(root.left);
        getMinimumDifference(root.right);
        return ret;
    }
}